<template>
    <div>
        <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>商品管理</el-breadcrumb-item>
            <el-breadcrumb-item>商品列表</el-breadcrumb-item>
        </el-breadcrumb>

        <!--        卡片视图区域-->
        <el-card>
            <el-row>
                <el-col>
                    <el-button type="primary" @click="addCate">添加分类</el-button>
                </el-col>
            </el-row>

            <!--            表格区域-->
            <tree-table :data="cateList" :columns="columns" class="treeTable"
                        :selection-type="false" :expand-type="false" show-index :show-row-hover="false">
                <template slot="isok" slot-scope="scope">
                    <i class="el-icon-success" v-if="scope.row.cat_deleted === false" style="color: lightgreen"></i>
                    <i class="el-icon-error" v-else style="color: red"></i>
                </template>
                <template slot="order" slot-scope="scope">
                    <el-tag v-if="scope.row.cat_level === 0">一级</el-tag>
                    <el-tag v-else-if="scope.row.cat_level === 1" type="success">二级</el-tag>
                    <el-tag v-else type="warning">三级</el-tag>
                </template>
                <template slot="opation" slot-scope="scope">
                    <el-button type="primary" class="el-icon-edit">编辑</el-button>
                    <el-button type="danger" class="el-icon-delete" @click="cateDelete(scope.row)">删除</el-button>
                </template>
            </tree-table>

            <!--            分页区域-->
            <el-pagination
                    @size-change="cateHandleSizeChange"
                    @current-change="cateHandleCurrentChange"
                    :current-page="queryInfo.pagenum"
                    :page-sizes="[1,2,3,10]"
                    :page-size="queryInfo.pagesize"
                    layout="total, sizes, prev, pager, next, jumper"
                    :total="total">
            </el-pagination>

            <!--            添加分类弹框-->
            <el-dialog
                    :visible.sync="addCateDialogVisible"
                    title="添加用户"
                    width="50%"
                    @close="addDialog">
                <el-form :model="addCateForm" :rules="addCateFormRules" label-width="80px" ref="addCateForm">
                    <el-form-item label="分类名称" prop="cat_name">
                        <el-input v-model="addCateForm.cat_name"></el-input>
                    </el-form-item>
                    <el-form-item label="父级分类">
                        <!--opations用来指定数据源  props用来指定配置的对象-->
                        <el-cascader style="width:100%;" v-model="selectKeys"
                                     :props="catederProps" :options="parentList"
                                     expand-trigger="hover" clearable @change="cascaderChange"
                                     change-on-select>
                        </el-cascader>
                    </el-form-item>
                </el-form>
                <span class="dialog-footer" slot="footer">
                <el-button @click="addCateDialogVisible = false">取 消</el-button>
                <el-button @click="addCateReady" type="primary">确 定</el-button>
                </span>
            </el-dialog>
        </el-card>
    </div>
</template>

<script>
  import TreeTable from 'vue-table-with-tree-grid'

  export default {
    name: "Categories",
    components: {
      TreeTable
    },
    data() {
      return {
        cateList: [],
        queryInfo: {//查询条件
          type: 3,
          pagenum: 1,
          pagesize: 5,
        },
        total: 0,
        columns: [{
          label: '分类名称',
          prop: 'cat_name',
        },
          {
            label: '分类名称',
            //表示当前列定义为模板列
            type: 'template',
            template: 'isok' //表示使用的模板列的名称
          },
          {
            label: '排序',
            //表示当前列定义为模板列
            type: 'template',
            template: 'order' //表示使用的模板列的名称
          },
          {
            label: '操作',
            //表示当前列定义为模板列
            type: 'template',
            template: 'opation' //表示使用的模板列的名称
          },
        ],
        //添加分类的表单绑定数据对象
        addCateForm: {
          cat_name: '',
          cat_pid: 0, //父级分类的id
          cat_level: 0,//分类的等级
        },
        addCateFormRules: {
          cat_name: [{required: true, message: '请输入分类名称', trigger: 'blur'}],
        },
        addCateDialogVisible: false,
        parentList: [],
        selectKeys: [],//选中的父级分类id值  会存放在这个数组里
        catederProps: {//指定配置的对象
          value: 'cat_id',
          label: 'cat_name',
          children: 'children'
        }

      }
    },
    methods: {
      //获取所有的商品分类数据列表
      async getCateList() {
        const {data: res} = await this.$http.get('categories', {params: this.queryInfo})
        if (res.meta.status !== 200) return this.$message.error('获取商品列表失败')
        this.cateList = res.data.result
        this.total = res.data.total  //赋值总数据条数
        // console.log(this.cateList)
        // console.log(res.data);
      },
      //获取父级分类里面分类数据
      async getParentList() {
        const {data: res} = await this.$http.get('categories', {params: {type: 2}})
        if (res.meta.status !== 200) return
        this.parentList = res.data
        console.log(this.parentList)
      },
      cateHandleSizeChange(newSize) {
        //监听pagesize的值的改变
        this.queryInfo.pagesize = newSize
        this.getCateList()
      },
      cateHandleCurrentChange(newpage) {
        // 监听pagenum的变化
        this.queryInfo.pagenum = newpage
        this.getCateList()
      },
      //添加分类按钮的点击
      addCate() {
        this.getParentList()
        this.addCateDialogVisible = true
      },
      //级联选择器的值改变时触发
      cascaderChange() {
        //如果this.selectKeys中数组长度大于0,则用户有选中父级分类
        //如果this.selectKeys数组长度为0 ,则用户没有选中任何父级分类 需要弹出一个错误提示
        if (this.selectKeys.length > 0) {
          //更新父级分类的Id
          this.addCateForm.cat_pid = this.selectKeys[this.selectKeys.length - 1]
          //更新父级分类的等级
          this.addCateForm.cat_level = this.selectKeys.length
          return
        } else {
          //重置父级分类的ID和等级
          this.addCateForm.cat_pid = 0
          this.addCateForm.cat_level = 0
        }
      },
      async addCateReady() {
        // 添加分类弹框确定点击
        const {data: res} = await this.$http.post('categories', this.addCateForm)
        if (res.meta.status !== 201) return
        this.$message.success('添加分类成功')
        this.getCateList()//重新搜索商品列表
        this.addCateDialogVisible = false
      },
      addDialog() {
        this.$refs.addCateForm.resetFields()//对话框关闭 重置表单
        //重置级联选择器保存数据的数组
        this.selectKeys = [],
          this.addCateForm.cat_pid = 0
        this.addCateForm.cat_level = 0
      },
      //删除按钮的点击
      async cateDelete(cate){
        const {data:res} = await  this.$http.delete(`categories/${cate.cat_id}`)
        if (res.meta.status !== 200 )return  this.$message.error('删除商品分类失败')
        this.$message.success('删除商品分类成功')
        this.getCateList()
      }
    },
    created() {
      this.getCateList()
    }
  }
</script>

<style scoped>
    .treeTable {
        margin-top: 15px;
    }
</style>